<!-- index.vue -->
<template>
    <div class="department">
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['dept.dept/add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
                <el-button @click="handleExpand"> 展开/折叠 </el-button>
            </div>
            <el-table
                    ref="tableRef"
                    class="mt-4"
                    size="large"
                    v-loading="loading"
                    :data="lists"
                    row-key="id"
                    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
                <el-table-column
                        label="类型名称"
                        prop="typeName"
                        min-width="150"
                        show-overflow-tooltip
                />

                <el-table-column label="排序" prop="px" min-width="100" />
                <el-table-column label="标识" prop="typeCode" min-width="100" />
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="更新时间" prop="update_time" min-width="180" />
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">

                        <el-button
                                v-perms="['dept.dept/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                                v-if="row.pid !== 0"
                                v-perms="['dept.dept/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="department">
    import type { ElTable, FormInstance } from 'element-plus'
    import EditPopup from './edit.vue'
    import { comcontactstypeDelete, comcontactstypeList } from '@/api/comcontacts/comcontacts'
    import feedback from '@/utils/feedback'
    const tableRef = shallowRef<InstanceType<typeof ElTable>>()
    const editRef = shallowRef<InstanceType<typeof EditPopup>>()
    const formRef = shallowRef<FormInstance>()
    let isExpand = false
    const loading = ref(false)
    const lists = ref<any[]>([])
    const queryParams = reactive({

    })
    const showEdit = ref(false)
    const getLists = async () => {
        loading.value = true
        let dataList = await comcontactstypeList(queryParams)
        lists.value = dataList.lists
        loading.value = false
    }

    const resetParams = () => {
        formRef.value?.resetFields()
        getLists()
    }

    const handleAdd = async (id?: number) => {
        showEdit.value = true
        await nextTick()
        if (id) {
            editRef.value?.setFormData({
                pid: id
            })
        }
        editRef.value?.open('add')
    }

    const handleEdit = async (data: any) => {
        showEdit.value = true
        await nextTick()
        editRef.value?.setFormData(data) // 设置表单数据
        editRef.value?.open('edit')
    }

    const handleDelete = async (id: number) => {
        await feedback.confirm('确定要删除？')
        await comcontactstypeDelete({ id })
        getLists()
    }

    const handleExpand = () => {
        isExpand = !isExpand
        toggleExpand(lists.value, isExpand)
    }

    const toggleExpand = (children: any[], unfold = true) => {
        for (const key in children) {
            tableRef.value?.toggleRowExpansion(children[key], unfold)
            if (children[key].children) {
                toggleExpand(children[key].children!, unfold)
            }
        }
    }

    onMounted(async () => {
        await getLists()
        nextTick(() => {
            handleExpand()
        })
    })
</script>
